package com.hskn.hss.module.carTrack.service;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.carTrack.dto.CarTrackDto;
import com.hskn.hss.module.carTrack.entity.CarTrack;
import com.hskn.hss.module.carTrack.to.AlarmAndViolationCarTo;
import com.hskn.hss.module.carTrack.to.MileageAndOilWearTo;
import com.hskn.hss.module.carTrack.vo.AlarmAndViolationCar;
import com.hskn.hss.module.carTrack.to.CleanListTo;
import com.hskn.hss.module.carTrack.vo.CleanPointVo;
import com.hskn.hss.module.carTrack.vo.FaclCleanListVo;
import com.hskn.hss.module.carTrack.vo.MileageAndOilWearVo;
import com.hskn.hss.module.carTrackOriginal.to.CarTrackGridTo;
import com.hskn.hss.module.common.to.OilNumTO;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.oilDevice.domain.StateOilDevice;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
public interface CarTrackService extends IService<CarTrack> {


    List mList(JSONObject params) throws HssException;

    AjaxResult insert(JSONObject params);

    JSONObject selectPage(JSONObject params) throws HssException;

    List<Grid> getCarTrackGrid(CarTrackGridTo carTrackGridTo);

    List getLiveLjqyCar(JSONObject params) throws HssException;

    List violationsAndAlarms(JSONObject params) throws HssException;

    CleanPointVo cleanPoint();

    MileageAndOilWearVo mileageAndOilWear(MileageAndOilWearTo mileageAndOilWearTo) throws HssException;

    AlarmAndViolationCar alarmAndViolationCarList(AlarmAndViolationCarTo alarmAndViolationCarTo) throws HssException;

    CarTrackDto lastCarTrack(CarTrackDto carTrackDto) throws HssException;

    FaclCleanListVo cleanList(CleanListTo cleanListTo) throws HssException;

    List<StateOilDevice> getStateOilDevices(String carId, Date date);

    BigDecimal getAlarmOilWearNum(List<StateOilDevice> stateOilDeviceList, BigDecimal factor2);

    List<CarTrack> getCarTracks(String carId, Date date);

    BigDecimal getAlarmOilWearNum(String carId, List<CarTrack> carTrackListBeginServenDay, BigDecimal factor2);

    void updateOilAndTimeConsuming(MileageAndOilWearTo mileageAndOilWearTo);

    void updateRptCarWorkByWorkType();

    //    @Override
//    public void insertRptCarWork(MileageAndOilWearTo mileageAndOilWearTo) {
//        try {
//            log.info("==================车辆考勤-初始化=================start=");
//            /** 车辆考勤 **/
////            List<TlCar> tlCarList = iTlCarService.list();
////            for (TlCar tlCar : tlCarList) {
////                tlCar.setAttendance(1);
////                tlCar.setShiftStatus(1);
////                tlCar.setOnguardstatus(1);
////                tlCarMapper.updateById(tlCar);
////            }
//            List allPlans = new ArrayList<>();
//
//            Set<String> gridIdList = new HashSet<>();
//            // 1.查询机械清扫、垃圾清运、垃圾转运作业
//            Date now = new Date();
//            String today = mileageAndOilWearTo.getWorkDate();
//            now = DateUtil.parse(today);
//            QueryWrapper<JxqsPlan> jxqsWrapper = new QueryWrapper<>();
//            jxqsWrapper.lambda().le(JxqsPlan::getYxsdStart, today);
//            jxqsWrapper.lambda().ge(JxqsPlan::getYxsdEnd, today);
//            jxqsWrapper.lambda().eq(JxqsPlan::getIsEnable, "1");
//            List<JxqsPlan> jxqsPlanList = jxqsPlanService.list(jxqsWrapper);
//            for (JxqsPlan jxqsPlan : jxqsPlanList) {
//                boolean exist = TimeUtil.checkPeriodDate(now, StringUtils.StringToDate3(jxqsPlan.getYxsdStart()), StringUtils.StringToDate3(jxqsPlan.getYxsdEnd()), jxqsPlan.getWorkPeriod());
//                if (exist) {
//                    allPlans.add(jxqsPlan);
//                }
//            }
//            QueryWrapper<LjqyWorkPlan> ljqyWrapper = new QueryWrapper<>();
//            ljqyWrapper.lambda().le(LjqyWorkPlan::getYxsdStartDate, today);
//            ljqyWrapper.lambda().ge(LjqyWorkPlan::getYxsdEndDate, today);
//            ljqyWrapper.lambda().ge(LjqyWorkPlan::getIsEnable, "1");
//            List<LjqyWorkPlan> ljqyWorkPlanList = ljqyWorkPlanService.list(ljqyWrapper);
//            for (LjqyWorkPlan ljqyWorkPlan : ljqyWorkPlanList) {
//                boolean exist = TimeUtil.checkPeriodDate(now, StringUtils.StringToDate3(ljqyWorkPlan.getYxsdStartDate()), StringUtils.StringToDate3(ljqyWorkPlan.getYxsdEndDate()), ljqyWorkPlan.getWorkCycle());
//                if (exist) {
//                    allPlans.add(ljqyWorkPlan);
//                }
//            }
//            QueryWrapper<LjzyWorkPlan> ljzyWrapper = new QueryWrapper<>();
//            ljzyWrapper.lambda().le(LjzyWorkPlan::getYxsdStartDate, today);
//            ljzyWrapper.lambda().ge(LjzyWorkPlan::getYxsdEndDate, today);
//            List<LjzyWorkPlan> ljzyWorkPlanList = ljzyWorkPlanService.list(ljzyWrapper);
//            for (LjzyWorkPlan ljzyWorkPlan : ljzyWorkPlanList) {
//                boolean exist = TimeUtil.checkPeriodDate(now, StringUtils.StringToDate3(ljzyWorkPlan.getYxsdStartDate()), StringUtils.StringToDate3(ljzyWorkPlan.getYxsdEndDate()), 1);
//                if (exist) {
//                    allPlans.add(ljzyWorkPlan);
//                }
//            }
//
//            List<String> carIds = new ArrayList<>();
//            // 2.遍历计划
//            for (Object plan : allPlans) {
//                if (plan.getClass().equals(JxqsPlan.class)) {
//                    JxqsPlan jxqsPlan = (JxqsPlan) plan;
//                    String carIdsStr = jxqsPlan.getCarIds();
//                    String[] carSplit = carIdsStr.split(",");
//                    for (String carId : carSplit) {
//                        carIds.add(carId);
//                    }
//                    //如果可以查询车辆id 说明有排班
//                    List<TlCar> tlCarShiftList = tlCarMapper.selectInIdCarIds(carIds);
//                    for (TlCar tlCar : tlCarShiftList) {
//                        tlCar.setShiftStatus(0);
//                        tlCarMapper.updateById(tlCar);
//                    }
//
//                    if (StringUtils.isEmpty(carIdsStr)) {
//                        continue;
//                    }
//                    Grid grid = gridMapper.selectById(jxqsPlan.getGridId());
//                    if (null == grid) {
//                        log.error("---不存在此网格，不初始化车辆考勤表，gridId:{},jxqsPlanId:{}，carIdsStr:{}", jxqsPlan.getGridId(), jxqsPlan.getJxqsPlanId(), carIdsStr);
//                        continue;
//                    }
//                    gridIdList.add(jxqsPlan.getGridId());
//                    RptCarWork rptCarWork = new RptCarWork();
//                    rptCarWork.setPlanId(jxqsPlan.getJxqsPlanId());
//                    rptCarWork.setWorkCycle(jxqsPlan.getWorkPeriod());
//                    rptCarWork.setPlanName(jxqsPlan.getName());
//                    rptCarWork.setGridId(jxqsPlan.getGridId());
//                    rptCarWork.setGridName(jxqsPlan.getGridName());
//                    rptCarWork.setGroupId(jxqsPlan.getWorkGroupId());
//                    rptCarWork.setGroupName(jxqsPlan.getWorkGroupName());
//                    rptCarWork.setRuleId(jxqsPlan.getWorkRuleId());
//                    rptCarWork.setRuleName(jxqsPlan.getWorkRuleName());
//                    rptCarWork.setExpectOilConsumption(null == jxqsPlan.getDcyjyh() ? BigDecimal.ZERO : jxqsPlan.getDcyjyh());
//                    rptCarWork.setExpectWaterConsumption(null == jxqsPlan.getDcyjsh() ? BigDecimal.ZERO : jxqsPlan.getDcyjsh());
//                    rptCarWork.setExpectWorkTime(new BigDecimal(null == jxqsPlan.getDczysc() ? 0 : jxqsPlan.getDczysc()));
//                    rptCarWork.setWorkType(2);
//                    //计划总进度，机械清扫为总里程，其他为收集点数量
//                    Double km = null == grid.getKm() ? 0 : grid.getKm();
//
//                    Integer tangCi = null == jxqsPlan.getTangci() ? 0 : jxqsPlan.getTangci();
//                    rptCarWork.setAllProgress(km.intValue() * tangCi);
//                    rptCarWork.setWorkTypeId(jxqsPlan.getWorkTypeId());
//                    rptCarWork.setWorkTypeName(jxqsPlan.getWorkTypeName());
//                    rptCarWork.setCarWorkType(jxqsPlan.getType());
//                    rptCarWork.setTangCi(tangCi);
//                    handlePlanAndSave(now, rptCarWork, jxqsPlan.getWorkGroupId(), carIdsStr);
//                } else if (plan.getClass().equals(LjqyWorkPlan.class)) {
//                    LjqyWorkPlan ljqyWorkPlan = (LjqyWorkPlan) plan;
//                    String carId = ljqyWorkPlan.getWorkCarId();
//
//                    //如果可以查询车辆id 说明有排班
////                    QueryWrapper<TlCar> tlCarWrapper = new QueryWrapper<>();
////                    tlCarWrapper.lambda().eq(TlCar::getId, carId);
//                    TlCar tlCar = tlCarMapper.selectById(carId);
//                    if (null != tlCar) {
//                        tlCar.setShiftStatus(0);
//                        tlCarMapper.updateById(tlCar);
//                    }
//                    if (StringUtils.isEmpty(carId)) {
//                        continue;
//                    }
//                    RptCarWork rptCarWork = new RptCarWork();
//                    gridIdList.add(ljqyWorkPlan.getGridId());
//                    rptCarWork.setPlanId(ljqyWorkPlan.getLjqyWorkPlanId());
//                    rptCarWork.setWorkCycle(ljqyWorkPlan.getWorkCycle());
//                    rptCarWork.setSjdIds(ljqyWorkPlan.getSjdIds());
//                    rptCarWork.setPlanName(ljqyWorkPlan.getName());
//                    rptCarWork.setGridId(ljqyWorkPlan.getGridId());
//                    rptCarWork.setGridName(ljqyWorkPlan.getGridName());
//                    rptCarWork.setGroupId(ljqyWorkPlan.getWorkGroupId());
//                    rptCarWork.setGroupName(ljqyWorkPlan.getWorkGroupName());
//                    rptCarWork.setRuleId(ljqyWorkPlan.getWorkRuleId());
//                    rptCarWork.setRuleName(ljqyWorkPlan.getWorkRuleName());
//                    rptCarWork.setExpectWorkTime(new BigDecimal(null == ljqyWorkPlan.getDczysc() ? 0 : ljqyWorkPlan.getDczysc()));
//                    rptCarWork.setWorkType(3);
//                    rptCarWork.setAllProgress(ljqyWorkPlan.getSjdIds().split(",").length);
//                    handlePlanAndSave(now, rptCarWork, ljqyWorkPlan.getWorkGroupId(), carId);
//
//                } else if (plan.getClass().equals(LjzyWorkPlan.class)) {
//                    LjzyWorkPlan ljzyWorkPlan = (LjzyWorkPlan) plan;
//                    String carId = ljzyWorkPlan.getCarId();
//
//                    //如果可以查询车辆id 说明有排班
////                    QueryWrapper<TlCar> tlCarWrapper = new QueryWrapper<>();
////                    tlCarWrapper.lambda().eq(TlCar::getId, carId);
//
//                    TlCar tlCar = tlCarMapper.selectById(carId);
//                    if (null != tlCar) {
//                        tlCar.setShiftStatus(0);
//                        tlCarMapper.updateById(tlCar);
//                    }
//                    if (StringUtils.isEmpty(carId)) {
//                        continue;
//                    }
//                    gridIdList.add(ljzyWorkPlan.getGridId());
//                    RptCarWork rptCarWork = new RptCarWork();
//                    rptCarWork.setPlanId(ljzyWorkPlan.getLjzyWorkPlanId());
//                    rptCarWork.setPlanName(ljzyWorkPlan.getName());
//                    rptCarWork.setGridId(ljzyWorkPlan.getGridId());
//                    rptCarWork.setGridName(ljzyWorkPlan.getGridName());
//                    rptCarWork.setGroupId(ljzyWorkPlan.getWorkGroupId());
//                    rptCarWork.setGroupName(ljzyWorkPlan.getWorkGroupName());
//                    rptCarWork.setRuleId(ljzyWorkPlan.getWorkRuleId());
//                    rptCarWork.setRuleName(ljzyWorkPlan.getWorkRuleName());
//                    rptCarWork.setWorkType(4);
//                    rptCarWork.setAllProgress(ljzyWorkPlan.getZzsjzIds().split(",").length);
//                    handlePlanAndSave(now, rptCarWork, ljzyWorkPlan.getWorkGroupId(), carId);
//                }
//            }
//            /** 复刻网格信息 **/
//            QueryWrapper<Grid> gridQueryWrapper = new QueryWrapper<>();
//            gridQueryWrapper.lambda().in(Grid::getGridId, gridIdList);
//            List<Grid> gridList = gridMapper.selectList(gridQueryWrapper);
//            gridList.forEach(grid -> {
//                GridSnapshot gridSnapshot = new GridSnapshot();
//                gridSnapshot.setId(StringUtils.uuid());
//                gridSnapshot.setDate(StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
//                BeanUtils.copyProperties(grid, gridSnapshot);
//                gridSnapshot.setCreateTime(new Date());
//                gridSnapshot.setUpdateTime(new Date());
//                gridSnapshotMapper.insert(gridSnapshot);
//                System.out.println("=======================");
//            });
//        } catch (ParseException e) {
//            e.printStackTrace();
//            log.error("ERROR", e);
//        }
//
//    }
//
//    private void handlePlanAndSave(Date now, RptCarWork rptCarWork, String workGroupId, String carIdsStr) {
//        WorkGroup workGroup = workGroupMapper.selectById(workGroupId);
//        if (null != workGroup) {
//            //勤类型，固定班1，冬夏班2
//            Integer kaoqinType = workGroup.getKaoqinType();
//            //1:夏季,2:冬季
//            String season = DateUtils.getCurrentSeason(now);
//            String kaoqinIds = workGroup.getKaoqinIds();
//            if (StringUtils.isEmpty(kaoqinIds)) {
//                return;
//            }
//            List<WorkKaoqin> workKaoqinList = workKaoqinMapper.selectBatchIds(Arrays.asList(kaoqinIds.split(",")));
//            String[] carIds = carIdsStr.split(",");
//            for (String carId : carIds) {
//                String workDate = DateUtil.format(now, "yyyy-MM-dd");
//
//                //重复判断
//                QueryWrapper<RptCarWork> rptCarWrokWrapper = new QueryWrapper<>();
//                rptCarWrokWrapper.lambda().eq(RptCarWork::getCarId, carId);
//                rptCarWrokWrapper.lambda().eq(RptCarWork::getPlanId, rptCarWork.getPlanId());
//                rptCarWrokWrapper.lambda().eq(RptCarWork::getWorkDate, workDate);
//                List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWrokWrapper);
//                if (!org.springframework.util.CollectionUtils.isEmpty(rptCarWorkList)) {
//                    continue;
//                }
//
//                TlCar tlCar = tlCarMapper.selectByIdWithCarType(carId);
//                if (null == tlCar) {
//                    continue;
//                }
//                for (WorkKaoqin workKaoqin : workKaoqinList) {
//                    try {
//                        String planStartTime = null;//排班开始时间
//                        String planEndTime = null;//排班结束时间
//                        if (1 == kaoqinType) {//固定班
//                            planStartTime = workKaoqin.getSbsj();
//                            planEndTime = workKaoqin.getXbsj();
//                        } else if (2 == kaoqinType) {//冬夏班
//                            if ("summer".equals(season)) {
//                                planStartTime = workKaoqin.getXsbsj();
//                                planEndTime = workKaoqin.getXxbsj();
//                            } else if ("winter".equals(season)) {
//                                planStartTime = workKaoqin.getDsbsj();
//                                planEndTime = workKaoqin.getDxbsj();
//                            }
//                        }
//                        rptCarWork.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//                        rptCarWork.setCarId(carId);
//                        rptCarWork.setCarNum(tlCar.getCarnumber());
//                        rptCarWork.setCarType(tlCar.getCarTypeName());
//                        rptCarWork.setWorkDate(workDate);//排班日期
//                        rptCarWork.setWorkMonth(DateUtil.format(now, "yyyy-MM"));//排班月份
//                        rptCarWork.setDepartmentId(tlCar.getDeptid());
//                        System.out.println(workDate + " " + planStartTime);
//                        if (null != planStartTime) {
//                            rptCarWork.setPlanStartTime(DateUtil.parse(workDate + " " + planStartTime, "yyyy-MM-dd HH:mm"));
//                        }
//                        if (null != planEndTime) {
//                            rptCarWork.setPlanEndTime(DateUtil.parse(workDate + " " + planEndTime, "yyyy-MM-dd HH:mm"));
//                        }
//                        rptCarWork.setCreateTime(now);
//                        rptCarWork.setCarName(tlCar.getCarnumber() + "[" + tlCar.getCarTypeName() + "]");
//                        rptCarWork.setShiftId(workKaoqin.getWorkKaoqinId());
//                        rptCarWork.setShiftName(workKaoqin.getName());
////                        rptCarWork.set
//                        // 保存车辆考勤
//                        rptCarWorkMapper.insert(rptCarWork);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        log.error("ERROR", e);
//                    }
//                }
//            }
//        }
//    }
    void processingStopPointCarTrack(OilNumTO oilNumTO);

    void supplementaryCarTrackPoints(OilNumTO oilNumTO);


//    void insertRptCarWork(MileageAndOilWearTo mileageAndOilWearTo);
}
